package com.e.takeaway.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.e.takeaway.R;
import com.e.takeaway.po.Food;
import com.e.takeaway.po.Order;
import com.squareup.picasso.Picasso;

import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Random;

public class OrderAdapter extends BaseAdapter {

    private List<Order> orders;
    private Context ctr;
    private LayoutInflater inflater;
    public OrderAdapter(Context context,List<Order> list){
        ctr = context;
        this.orders = list;
//        System.out.println("dddddd: " + this.orders);
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return orders.size();
    }

    @Override
    public Object getItem(int i) {
        return orders.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @SuppressLint("SetTextI18n")
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Order order = orders.get(i);
        OrderAdapter.ViewHolder viewHolder = null;
        if (view == null){
            view  = inflater.inflate(R.layout.item_order,viewGroup,false);
            viewHolder = new ViewHolder();
            viewHolder.order_time = view.findViewById(R.id.order_time);
            viewHolder.order_user = view.findViewById(R.id.order_user);
            viewHolder.order_img = view.findViewById(R.id.order_image);
            viewHolder.order_name =view.findViewById(R.id.order_shop1);
            viewHolder.order_number = view.findViewById(R.id.order_num);
            viewHolder.order_price = view.findViewById(R.id.order_sum);
            viewHolder.order_id = view.findViewById(R.id.order_id);
            viewHolder.order_status = view.findViewById(R.id.order_status);
            viewHolder.order_phone = view.findViewById(R.id.order_phone);
            view.setTag(viewHolder);
        }else{
            viewHolder = (OrderAdapter.ViewHolder)view.getTag();
        }
//        System.out.println(order);
       if (null!=order){
           Picasso.with(ctr)
                   .load(order.getMerimg())
                   .placeholder(viewHolder.order_img.getDrawable())
                   .error(R.drawable.lunbo1)
                   .into(viewHolder.order_img);
           viewHolder.order_id.setText("1");
           SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
           String format = dateFormat.format(order.getCreateTime());
           viewHolder.order_time.setText(format);
           viewHolder.order_user.setText(order.getName());
           viewHolder.order_number.setText(order.getSnumber());
           viewHolder.order_price.setText(order.getSum());
           viewHolder.order_phone.setText(order.getPhone());
           if (order.getStatus()==1){
               viewHolder.order_status.setText("已支付");
           }
           int i1 = new Random().nextInt(99999);
           viewHolder.order_id.setText(String.valueOf(i1));

           viewHolder.order_name.setText(order.getMername());
       }
        return view;
    }

    static class ViewHolder{
        public ImageView order_img;
        public TextView order_name;
        public TextView order_price;
        public TextView order_id;
        public TextView order_time;
        public TextView order_user;
        public TextView order_phone;
        public TextView order_number;
        public TextView order_status;
        public Button btn_delete;
    }
}
